package Thread;
//阻塞队列的实现
class MyBlockingQueue{
    //定义一个循环数组
    private String[] queue = new String[1000];//队列中有效元素的范围尾[0,1000),这样当head=tail的时候，队列为空
    private volatile int head = 0;//下标，队列头
    private volatile int tail = 0;//队列尾下标
    private volatile int size = 0;//队列的元素个数
    //入队列
    public void put(String elem){
        synchronized (this){
            while (size == queue.length){
                //队列满了
//                return;
                //实现阻塞
                try {
                    this.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            //队列没满
            queue[tail] = elem;
            tail++;
            if (tail >= queue.length){
                tail = 0;
            }
            size++;
            //唤醒队列为空出队列的阻塞状态
            this.notify();
        }

    }
    //出队列
    public String take(){
        synchronized (this){
            while (size == 0){
                //队列为空
//                return null;
                //实现阻塞
                try {
                    this.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            //队列不为空
            String elem = queue[head];
            head++;
            if (head >= queue.length){
                head = 0;
            }
            size--;
            //唤醒队列满了入队列的阻塞状态
            this.notify();
            return elem;
        }

    }
}
public class Demo19 {
    public static void main(String[] args) {
        //实现生产者消费者队列
        MyBlockingQueue myBlockingQueue = new MyBlockingQueue();
        Thread t1 = new Thread(()->{
            int count = 0;
            while (true){
                myBlockingQueue.put(count+"");
                System.out.println("生成元素："+ count);
                count++;
//                try {
//                    myBlockingQueue.put(count+"");
//                    System.out.println("生成元素："+ count);
//                    count++;
//                    Thread.sleep(1000);
//                } catch (InterruptedException e) {
//                    e.printStackTrace();
//                }
            }
        });
        Thread t2 = new Thread(()->{
            while (true){
                try {
                    Thread.sleep(1000);
                    String elem = myBlockingQueue.take();
                    System.out.println("消费元素："+ elem);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
        t1.start();
        t2.start();

//        MyBlockingQueue myBlockingQueue = new MyBlockingQueue();
//        myBlockingQueue.put("123");
//        myBlockingQueue.put("234");
//        myBlockingQueue.put("345");
//        System.out.println(myBlockingQueue.take());
//        System.out.println(myBlockingQueue.take());
//        System.out.println(myBlockingQueue.take());
//        System.out.println(myBlockingQueue.take());
    }

}
